home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / gblanker36_src.lha / GSource.lha / GSource / Blankers / ASwarm / blank.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-05  |  9.4 KB  |  368 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include "/includes.h"
  11. #include <stdlib.h>
  12.  
  13. #define MAX_SPEED 6L
  14.  
  15. #define SPEED 0
  16. #define COLOR 1
  17. #define AIM   2
  18. #define BEES  4
  19. #define WASPS 6
  20. #define BACC  8
  21. #define WACC  10
  22. #define MODE  12
  23.  
  24. #define MAXBEEVEL ( 3 * mP[BACC].po_Level )
  25. #define MAXWASPVEL ( 4 * mP[WACC].po_Level )
  26.  
  27. #define BORDER 5
  28.  
  29. #define BEE_PEN 1
  30. #define WASP_PEN 2
  31.  
  32. #define BEE_COL_NUM 33
  33.  
  34. #define RAND( m ) ( RangeRand( m ) - ( m ) / 2 )
  35.  
  36. #define BXVel( I ) ( SP->ss_X[3][I] )
  37. #define BYVel( I ) ( SP->ss_Y[3][I] )
  38. #define BeeX( P, I ) ( SP->ss_X[P][I] )
  39. #define BeeY( P, I ) ( SP->ss_Y[P][I] )
  40. #define MyWasp( I ) ( SP->ss_MW[I] )
  41. #define WaXVel( I ) ( SP->ss_WX[3][I] )
  42. #define WaYVel( I ) ( SP->ss_WY[3][I] )
  43. #define WaspX( P, I ) ( SP->ss_WX[P][I] )
  44. #define WaspY( P, I ) ( SP->ss_WY[P][I] )
  45. #define SwarmSize( W, B ) ( sizeof( SwarmStruct ) + sizeof( LONG ) * ( W + B ) * 9L )
  46.  
  47. typedef struct _SwarmStruct
  48. {
  49.     LONG ss_Width;
  50.     LONG ss_Height;
  51.     LONG ss_NumWasps;
  52.     LONG *ss_WX[4];
  53.     LONG *ss_WY[4];
  54.     LONG *ss_NB;
  55.     LONG ss_NumBees;
  56.     LONG ss_BeeAcc;
  57.     LONG *ss_X[4];
  58.     LONG *ss_Y[4];
  59.     LONG *ss_MW;
  60. } SwarmStruct;
  61.  
  62. ULONG BeeColors[] =
  63. {
  64.     0x000F, 0x000F, 0x004F, 0x008F, 0x00BF, 0x00FF, 0x00FB, 0x00F7, 0x00F3,
  65.     0x00F0, 0x04F0, 0x08F0, 0x09F0, 0x0AF0, 0x0BF0, 0x0CF0, 0x0DF0, 0x0EF0,
  66.     0x0FF0, 0x0FE0, 0x0FD0, 0x0FC0, 0x0FB0, 0x0F90, 0x0F80, 0x0F70, 0x0F60,
  67.     0x0F50, 0x0F40, 0x0F30, 0x0F20, 0x0F10, 0x0F00
  68. };
  69.  
  70. UWORD SwarmColors[4] = { 0x0000, 0x0000, 0x0FFF, 0x0000 };
  71.  
  72. BYTE sqrt_tab[256] =
  73. {
  74.     0,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,
  75.     6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,
  76.     8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,
  77.     10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,
  78.     11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,
  79.     12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,
  80.     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,
  81.     14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
  82.     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  83.     15,15,15,15,15,15,15
  84.     };
  85.  
  86. #include "ASwarm_rev.h"
  87. STATIC const UBYTE VersTag[] = VERSTAG;
  88.  
  89. VOID Defaults( PrefObject *Prefs )
  90. {
  91.     Prefs[SPEED].po_Active = 6;
  92.     Prefs[COLOR].po_Active = 1;
  93.     Prefs[AIM].po_Active = 1;
  94.     Prefs[BEES].po_Level = 50;
  95.     Prefs[WASPS].po_Level = 4;
  96.     Prefs[BACC].po_Level = 6;
  97.     Prefs[WACC].po_Level = 4;
  98.     Prefs[MODE].po_ModeID = getTopScreenMode();
  99. }
  100.  
  101. LONG FastSQRT( LONG x )
  102. {
  103.     LONG sr = 1L;
  104.     
  105.     while( x > 255L )
  106.     {
  107.         x /= 4L;
  108.         sr *= 2L;
  109.     }
  110.     
  111.     return sr * ( LONG )sqrt_tab[x];
  112. }
  113.  
  114. SwarmStruct *CreateSwarms( struct Screen *Scr, PrefObject *mP )
  115. {
  116.     LONG Index, *Ptr;
  117.     SwarmStruct    *SP;
  118.  
  119.     if(( SP = AllocVec( SwarmSize( mP[WASPS].po_Level, mP[BEES].po_Level ),
  120.                        0L )) == NULL )
  121.         return NULL;
  122.     
  123.     SP->ss_NumWasps = mP[WASPS].po_Level;
  124.     SP->ss_NumBees = mP[BEES].po_Level;
  125.     SP->ss_Width = Scr->Width;
  126.     SP->ss_Height = Scr->Height;
  127.     SP->ss_BeeAcc = mP[BACC].po_Level;
  128.     
  129.     Ptr = ( LONG * )( &SP[1] );
  130.     for( Index = 0L; Index < 4L; Index++ )
  131.     {
  132.         SP->ss_WX[Index] = Ptr;
  133.         Ptr += mP[WASPS].po_Level;
  134.         SP->ss_WY[Index] = Ptr;
  135.         Ptr += mP[WASPS].po_Level;
  136.         SP->ss_X[Index] = Ptr;
  137.         Ptr += mP[BEES].po_Level;
  138.         SP->ss_Y[Index] = Ptr;
  139.         Ptr += mP[BEES].po_Level;
  140.     }
  141.     SP->ss_NB = Ptr;
  142.     SP->ss_MW = Ptr + mP[WASPS].po_Level;
  143.  
  144.     for( Index = 0L; Index < mP[WASPS].po_Level; Index++ )
  145.     {
  146.         WaspX( 1, Index ) = WaspX( 0, Index ) =    BORDER +
  147.             RangeRand( SP->ss_Width - 2 * BORDER );
  148.         WaspY( 1, Index ) = WaspY( 0, Index ) =    BORDER +
  149.             RangeRand( SP->ss_Height - 2 * BORDER );
  150.         WaXVel( Index ) = RAND( mP[WACC].po_Level );
  151.         WaYVel( Index ) = RAND( mP[WACC].po_Level );
  152.         SP->ss_NB[Index]=0;
  153.     }
  154.  
  155.     for( Index = 0L; Index < SP->ss_NumBees; Index++ )
  156.     {
  157.         BeeX( 1, Index ) = BeeX( 0, Index ) = BORDER +
  158.             RangeRand( SP->ss_Width - 2 * BORDER );
  159.         BeeY( 1, Index ) = BeeY( 0, Index ) = BORDER +
  160.             RangeRand( SP->ss_Height - 2 * BORDER );
  161.         BXVel( Index ) = RAND( SP->ss_BeeAcc );
  162.         BYVel( Index ) = RAND( SP->ss_BeeAcc );
  163.         SP->ss_NB[MyWasp( Index ) = Index % SP->ss_NumWasps]++;
  164.     }
  165.  
  166.     return SP;
  167. }
  168.  
  169. VOID DrawSwarms( struct RastPort *RP, SwarmStruct *SP, PrefObject *mP )
  170. {
  171.     LONG Index, Aimmode = mP[AIM].po_Active;
  172.  
  173.     for( Index = 0L; Index < SP->ss_NumWasps; Index++ )
  174.     {
  175.         WaspX( 2, Index ) = WaspX( 1, Index );
  176.         WaspX( 1, Index ) = WaspX( 0, Index );
  177.         WaspY( 2, Index ) = WaspY( 1, Index );
  178.         WaspY( 1, Index ) = WaspY( 0, Index );
  179.  
  180.         WaXVel( Index ) += RAND( mP[WACC].po_Level );
  181.         WaYVel( Index ) += RAND( mP[WACC].po_Level );
  182.  
  183.         if( WaXVel( Index ) > MAXWASPVEL )
  184.             WaXVel( Index ) = MAXWASPVEL;
  185.         if( WaXVel( Index ) < -MAXWASPVEL )
  186.             WaXVel( Index ) = -MAXWASPVEL;
  187.         if( WaYVel( Index ) > MAXWASPVEL )
  188.             WaYVel( Index ) = MAXWASPVEL;
  189.         if( WaYVel( Index ) < -MAXWASPVEL )
  190.             WaYVel( Index ) = -MAXWASPVEL;
  191.  
  192.         WaspX( 0, Index ) = WaspX( 1, Index ) + WaXVel( Index );
  193.         WaspY( 0, Index ) = WaspY( 1, Index ) + WaYVel( Index );
  194.  
  195.         if(( WaspX( 0, Index ) < BORDER )||
  196.            ( WaspX( 0, Index ) > SP->ss_Width-BORDER-1 ))
  197.         {
  198.             WaXVel( Index ) = -WaXVel( Index );
  199.             if( WaspX( 0, Index ) < BORDER )
  200.                 WaspX( 0, Index ) = BORDER;
  201.             else
  202.                 WaspX( 0, Index ) = SP->ss_Width-BORDER-1;
  203.         }
  204.  
  205.         if(( WaspY( 0, Index ) < BORDER )||
  206.            ( WaspY( 0, Index ) > SP->ss_Height-BORDER-1 ))
  207.         {
  208.             WaYVel( Index ) = -WaYVel( Index );
  209.             if( WaspY( 0, Index ) < BORDER )
  210.                 WaspY( 0, Index ) = BORDER;
  211.             else
  212.                 WaspY(0, Index ) = SP->ss_Height-BORDER-1;
  213.         }
  214.     }
  215.  
  216.     for( Index = 0L; Index < SP->ss_NumBees; Index++ )
  217.     {
  218.         LONG DX, DY, ChkIndex;
  219.         LONG Distance, NewDistance;
  220.  
  221.         BeeX( 2, Index ) = BeeX( 1, Index );
  222.         BeeX( 1, Index ) = BeeX( 0, Index );
  223.         BeeY( 2, Index ) = BeeY( 1, Index );
  224.         BeeY( 1, Index ) = BeeY( 0, Index );
  225.  
  226.         DX = WaspX( 1, MyWasp( Index )) - BeeX( 1, Index );
  227.         DY = WaspY( 1, MyWasp( Index )) - BeeY( 1, Index );
  228.         Distance = FastSQRT( DX * DX + DY * DY );
  229.         if( !Distance )
  230.             Distance = 1L;
  231.  
  232.         if( Aimmode )
  233.         {
  234.             for( ChkIndex = 0; ChkIndex <= SP->ss_NumWasps; ChkIndex++ )
  235.             {
  236.                 if( ChkIndex != MyWasp( Index ))
  237.                 {
  238.                     LONG NewDX, NewDY;
  239.  
  240.                     NewDX = WaspX( 1, ChkIndex ) - BeeX( 1, Index );
  241.                     NewDY = WaspY( 1, ChkIndex ) - BeeY( 1, Index );
  242.                     NewDistance = FastSQRT( NewDX * NewDX + NewDY * NewDY );
  243.                     if( Distance > NewDistance )
  244.                     {
  245.                         DX = NewDX;
  246.                         DY = NewDY;
  247.                         if(!( NewDistance ))
  248.                             Distance = 1L;
  249.                         else
  250.                             Distance = NewDistance;
  251.                         SP->ss_NB[MyWasp( Index )]--;
  252.                         SP->ss_NB[MyWasp( Index ) = ChkIndex]++;
  253.                     }
  254.                 }
  255.             }
  256.         }
  257.  
  258.         BXVel( Index ) +=( DX * SP->ss_BeeAcc ) / Distance + RAND( 3 );
  259.         BYVel( Index ) +=( DY * SP->ss_BeeAcc ) / Distance + RAND( 3 );
  260.  
  261.         if( BXVel( Index ) > MAXBEEVEL )
  262.             BXVel( Index ) = MAXBEEVEL;
  263.         if( BXVel( Index ) < -MAXBEEVEL )
  264.             BXVel( Index ) = -MAXBEEVEL;
  265.         if( BYVel( Index ) > MAXBEEVEL )
  266.             BYVel( Index ) = MAXBEEVEL;
  267.         if( BYVel( Index ) < -MAXBEEVEL )
  268.             BYVel( Index ) = -MAXBEEVEL;
  269.  
  270.         BeeX( 0, Index ) = BeeX( 1, Index ) + BXVel( Index );
  271.         BeeY( 0, Index ) = BeeY( 1, Index ) + BYVel( Index );
  272.  
  273.         if(( BeeX( 0, Index ) < BORDER )||
  274.            ( BeeX( 0, Index ) > SP->ss_Width-BORDER-1 ))
  275.         {
  276.             BXVel( Index ) = -BXVel( Index );
  277.             BeeX( 0, Index ) = BeeX( 1, Index ) + BXVel( Index );
  278.         }
  279.  
  280.         if(( BeeY( 0, Index ) < BORDER )||
  281.            ( BeeY( 0, Index ) > SP->ss_Height-BORDER-1 ))
  282.         {
  283.             BYVel( Index ) = -BYVel( Index );
  284.             BeeY( 0, Index ) = BeeY( 1, Index ) + BYVel( Index );
  285.         }
  286.     }
  287.  
  288.     for( Index = 0L; Index < SP->ss_NumWasps; Index++ )
  289.     {
  290.         SetAPen( RP, 0 );
  291.         Move( RP, WaspX( 2, Index ), WaspY( 2, Index ));
  292.         Draw( RP, WaspX( 1, Index ), WaspY( 1, Index ));
  293.         SetAPen( RP, WASP_PEN );
  294.         Draw( RP, WaspX( 0, Index ), WaspY( 0, Index ));
  295.     }
  296.  
  297.     for( Index = 0L; Index < SP->ss_NumBees; Index++ )
  298.     {
  299.         SetAPen( RP, 0 );
  300.         Move( RP, BeeX( 2, Index ), BeeY( 2, Index ));
  301.         Draw( RP, BeeX( 1, Index ), BeeY( 1, Index ));
  302.         SetAPen( RP, BEE_PEN );
  303.         Draw( RP, BeeX( 0, Index ), BeeY( 0, Index ));
  304.     }
  305. }
  306.  
  307. LONG Blank( PrefObject *Prefs )
  308. {
  309.     LONG Count, Color, DColor, ScrToFrontCnt = 0, RetVal = OK;
  310.     struct Screen *SwarmScreen;
  311.     struct Window *Wnd;
  312.     SwarmStruct *Swarms;
  313.  
  314.     SwarmScreen = OpenScreenTags( 0L, SA_Depth, 2, SA_Overscan, OSCAN_STANDARD,
  315.                                  SA_DisplayID, Prefs[MODE].po_ModeID,
  316.                                  SA_Quiet, TRUE, SA_Behind, TRUE, TAG_DONE );
  317.     if( SwarmScreen )
  318.     {
  319.         SetRGB4( &( SwarmScreen->ViewPort ), 0, 0, 0, 0 );
  320.         SetRast( &( SwarmScreen->RastPort ), 0 );
  321.         Wnd = BlankMousePointer( SwarmScreen );
  322.         ScreenToFront( SwarmScreen );
  323.         Swarms = CreateSwarms( SwarmScreen, Prefs );
  324.         if( Swarms )
  325.         {
  326.             Color = BEE_COL_NUM-1;
  327.             DColor = Prefs[COLOR].po_Active ? 1 : 0;
  328.             Count = Prefs[SPEED].po_Level + 1;
  329.             
  330.             while( RetVal == OK )
  331.             {
  332.                 SwarmColors[BEE_PEN] = BeeColors[Color];
  333.                 LoadRGB4(&( SwarmScreen->ViewPort ), SwarmColors, 4 );
  334.                 Color += DColor;
  335.                 if(( Color == -1 )||( Color == BEE_COL_NUM ))
  336.                 {
  337.                     DColor = -DColor;
  338.                     Color += 2 * DColor;
  339.                 }
  340.                 
  341.                 WaitTOF();
  342.                 
  343.                 if(!( ScrToFrontCnt++ % 60 ))
  344.                     ScreenToFront( SwarmScreen );
  345.                     
  346.                 if( ++Count > MAX_SPEED )
  347.                 {
  348.                     Count = Prefs[SPEED].po_Level + 1;
  349.                     DrawSwarms(&( SwarmScreen->RastPort ), Swarms, Prefs );
  350.  
  351.                 }
  352.  
  353.                 RetVal = ContinueBlanking();
  354.             }
  355.             FreeVec( Swarms );
  356.         }
  357.         else
  358.             RetVal = FAILED;
  359.         
  360.         UnblankMousePointer( Wnd );
  361.         CloseScreen( SwarmScreen );
  362.     }
  363.     else
  364.         RetVal = FAILED;
  365.  
  366.     return RetVal;
  367. }
  368.